package sharing

import admin.User
import grails.transaction.Transactional

@Transactional
class UserService {

    /**
     * 分页组合查询
     * @return
     */
    def userInfo(map){
        def result=["code":0,"msg":"","count":0,"data":[]]
        def  hql="from User where 1=:one"
        def hmap=[one:1]
        if(map.userName!=null&&map.userName!=""){
            hql+=" and  userName like :userName"
            hmap<<[userName:"%"+map.userName.toString()?.trim()+"%"]
        }
        if(map.telephone){
            hql+=" and telephone like :phone";
            hmap<<[phone:"%"+map.telephone+"%"]
        }
        result.count=User.executeQuery("select  count(*) "+hql,hmap)[0]?:0
        def list
        if (map.page &&map.limit){
            def page=Integer.valueOf(map.page)
            def limit=Integer.valueOf(map.limit)
            def start=(page-1)*limit
            list=User.findAll(hql,hmap,[offset: start,max:limit])
        }else{
            list=User.findAll(hql,hmap)
        }
        list.each {User user->
            def temMap=[:]
            temMap.putAll(user.properties)

//  todo

            result.data<<temMap
        }
        return result
    }
    /**
     * 删除方法
     * @param map
     * @return
     */
    def delUser(map){
        def result=[sucess:true,errors:"",errorCode:"0"]
        if (!map.userId)
        {
            result.sucess=false
            result.errorCode="01"
            result.errors="xxxxx"
        }else{
            int userId=Integer.valueOf(map.userId)
            // one
//            User user=User.get(userId)
//            if (!user){
//                result.sucess=false
//                result.errorCode="02"
//                result.errors="yyyy"
//            }else{
//                user.delete()
//            }
            // two
            User.executeUpdate("delete  from User where userId in:idList",[idList:[userId]])

        }


//        List<User> list=User.findAllByUserId(userId)
//     int count=User.deleteAll(list)
        return result
    }
    /**
     * 修改回显
     * @param map
     * @return
     */
    def editById(map){
        int userId=map.userId
        User user=User.findByUserId(userId)
        return user
    }
    /**
     * 修改数据
     */
    def updateUser(map){
        def result=1
        int userId=Integer.valueOf(map.userId)
        User user=User.findByUserId(userId)
        if (user){
            user.properties=map
            if (!user.save(flush: true)){
                user.errors.each {
                    println(it)
                }
                result=0
            }else{
                result=1
            }
//            user.riq=Date.parse("yyyy-MM-dd",map.riqi)
        }else{
            result=0
        }
        return result
    }
    /**
     * 新增用户
     */
    def saveUser(map){
        def count=1
        User user =new User()
        user.properties=map
        if(!user.save()){
            user.errors.each {
                System.out.println(it)
            }
            count=0
        }

        return count
    }
}
